#include <REGX52.H>
/**
 * 灯一开始是常亮状态
 * 按一下变为闪烁。
 * 按一下熄灭。
 * 按一下常亮。
 * 三个状态切换
 *
 */
unsigned int state = 0;//取其三个状态
sbit LED = P3 ^ 1; 
sbit BTN = P3 ^ 7;

void delay_ms(unsigned int ms)
{
    unsigned int i, j;
    for (i = 0; i < ms; i++)
    {
        for (j = 0; j < 123; j++)
            ;
    }
}

void main()
{
    LED = 1; // 初始常亮
    while (1)
    {
        
        if (BTN == 0)
        {
            delay_ms(20); // 防止按得太快
            if (BTN == 0)
            {
                state = (state + 1) % 3;
                while (BTN == 0); 
            }
        }

        if (state == 0)
        {
            LED = 1; // 常亮
        }
        else if (state == 1)
        {
            LED = ~LED; // 闪烁
            delay_ms(300);
        }
        else if (state == 2)
        {
            LED = 0; // 熄灭
        }
    }
}